import { _decorator, Component, Node, Sprite, Vec3 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import EventManager from '../../Init/Managers/EventManager';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('LevelAwardBoxUI')
export class LevelAwardBoxUI extends BasicUI {
    @property(Node)
    normalSpLayer: Node = null;
    @property(Node)
    videoSpLayer: Node = null;

    @property(Node)
    normalLabels: Node = null;
    @property(Node)
    videoLabels: Node = null;

    awardType = 0;

    hideCb = null;
    isGetAward = false;

    show(hideCb) {
        this.onEvents();
        this.hideCb = hideCb;
        this.isGetAward = false;

        this.emit(EventTypes.GameEvents.GamePause);
        //随机奖励
        this.awardType = Math.floor(Math.random() * 4.5);
        console.log('awardType: ', GlobalEnum.BoxAwardTypes[this.awardType])

        this.setAwardSp();

        //直接默认点击视频
        if (GlobalTmpData.isShowMisPop) {
            this.node.active = false;
            // this.onVideoClick();
        } else {
            this.node.active = true;
        }

        // 
        EventManager.emit(EventTypes.GameEvents.GamePause);

        Adsdk.instance.hideAllAd();
        Adsdk.instance.showInterstitialAd();
        Adsdk.instance.showBanner();
        Adsdk.instance.showGridAdTop();
        Adsdk.instance.showGridAdRight();
        Adsdk.instance.showGridAdLeft();
    }

    setAwardSp() {
        for (let i = 0; i < this.normalSpLayer.children.length; i++) {
            this.normalSpLayer.children[i].active = this.awardType == i;
            this.videoSpLayer.children[i].active = this.awardType == i;
            this.normalLabels.children[i].active = this.awardType == i;
            this.videoLabels.children[i].active = this.awardType == i;
        }
    }

    hide() {
        super.hide();
        this.emit(EventTypes.GameEvents.GameResume);
        this.hideCb && this.hideCb(this.isGetAward);
    }

    /**获取奖励 */
    saveAward(isVideo = false) {
        let type = isVideo && this.awardType == 4 ? 5 : this.awardType;

        let cfg = AwardBoxCfg[type];
        let award = isVideo ? cfg.video : cfg.normal;

        switch (type) {
            case GlobalEnum.BoxAwardTypes.AddZombies:
                this.emit(EventTypes.RoleEvents.AddZombies, award, cfg.delay);
                break;
            case GlobalEnum.BoxAwardTypes.AddGold:
                let lv = StorageSystem.getData().levelAssets.curLv;
                award = Math.floor((lv * 0.2 + 1) * award);
                StorageSystem.setData((d) => {
                    d.userAssets.asset += award;
                }, true);
                StorageSystem.updateToAssets(true, false);

                break;
            case GlobalEnum.BoxAwardTypes.AddJumpDist:
                award = GlobalConfig.Zombie.atkDist * award;
                this.emit(EventTypes.RoleEvents.AddBuff,
                    { type: GlobalEnum.BuffType.AddJumpDist, num: award, delay: cfg.delay });
                break;
            case GlobalEnum.BoxAwardTypes.AddSpd:
                this.emit(EventTypes.RoleEvents.AddBuff,
                    { type: GlobalEnum.BuffType.AddSpeed, num: award, delay: cfg.delay });
                //效果
                // let d = { t: GlobalEnum.Effect2DType.GetSpdUp, p: Vec3.ZERO, delay: cfg.delay, isOne: true };
                // this.emit(EventTypes.EffectEvents.showUIEffect, d);
                break;
            case GlobalEnum.BoxAwardTypes.AddHP:
                this.emit(EventTypes.RoleEvents.AddAwardHP, award);
                break;
            case GlobalEnum.BoxAwardTypes.AddProtected:
                this.emit(EventTypes.RoleEvents.AddProtected, cfg.delay);
                if (!GlobalTmpData.BuffRecs[GlobalEnum.BuffType.Protected]) {
                    GlobalTmpData.BuffRecs[GlobalEnum.BuffType.Protected] = { num: cfg.delay, curt: 0, delay: cfg.delay };
                }
                GlobalTmpData.BuffRecs[GlobalEnum.BuffType.Protected].curt = 0;
                GlobalTmpData.BuffRecs[GlobalEnum.BuffType.Protected].delay = cfg.delay;
                GlobalTmpData.BuffRecs[GlobalEnum.BuffType.Protected].num = cfg.delay;
                break;
            default:
                break;
        }

        if (cfg && cfg.tip) {
            this.emit(EventTypes.GameEvents.ShowTips, cfg.tip);
        }
    }

    onVideoClick() {
        this.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                this.isGetAward = true;
                //观看视频成功
                this.saveAward(true);
                // 隐藏UI
                UISystem.hideUI(UIEnum.LevelAwardBoxUI);
            },
            fail: () => {
                this.isGetAward = false;
                // 隐藏UI
                UISystem.hideUI(UIEnum.LevelAwardBoxUI);
            },
            cancel: () => {
                this.isGetAward = false;
                // 隐藏UI
                UISystem.hideUI(UIEnum.LevelAwardBoxUI);
            },
        })
    }

    onNormalClick() {
        this.saveAward(false);
        // 隐藏UI
        UISystem.hideUI(UIEnum.LevelAwardBoxUI);
    }

    onCloseClick() {
        UISystem.hideUI(UIEnum.LevelAwardBoxUI);

        Adsdk.instance.hideAllAd();

    }
}

/**奖励配置 与 BoxAwardTypes 对应*/
export const AwardBoxCfg = {
    0: {
        type: GlobalEnum.BoxAwardTypes.AddZombies,
        normal: 1,
        video: 5,
        delay: 0,
        tip: '获得新的伙伴!',
    },
    1: {
        type: GlobalEnum.BoxAwardTypes.AddGold,
        normal: 400,
        video: 3000,
        delay: 0,
        tip: '获得大量金币!',
    },
    2: {
        type: GlobalEnum.BoxAwardTypes.AddJumpDist,
        normal: 0.5,
        video: 2,
        delay: 20,
        tip: '现在你跳的更远了!',
    },
    3: {
        type: GlobalEnum.BoxAwardTypes.AddSpd,
        normal: 0.3,
        video: 1.5,
        delay: 10,
        tip: '跑的起飞了!',
    },
    4: {
        type: GlobalEnum.BoxAwardTypes.AddHP,
        normal: 0.3,
        video: 1,
        delay: 10,
        tip: '治愈了你的伤势!',
    },
    5: {
        type: GlobalEnum.BoxAwardTypes.AddProtected,
        normal: 0,
        video: 0,
        delay: 10,
        tip: '获得护盾!',
    },
}